package com.smartrodin.springbootrabbitmq.util;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author: arnold_nie
 * @date: 2020/12/25 19:41
 * FileName: Task
 * Description:  延迟队列
 */
public class Task implements Delayed {

    /** 数据 */
    private final String data;

    /** 任务执行时间 */
    private final long time;

    public Task(String data, TimeUnit timeUnit, long time){
        this.data = data;
        this.time = System.currentTimeMillis() + timeUnit.toMillis(time);
    }

    public String getData() {
        return data;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long res= time - System.currentTimeMillis();
        return unit.convert(res, TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        if (o instanceof Task ){
            Task task = (Task) o;
            return (int) (this.time - task.time);
        }
        return 0;
    }
}
